<?php

class user extends Admin {

    private $status = array( 0 => '正常', 1 => '已锁定' );
    private $name = '用户';

    private $list = null;
    private $sort = null;

    function __construct() {
        parent::__construct();
        $this->list = $this->model->Cache()->get( 'user' );
        $this->sort = $this->sort( 'user' );
        $this->model->table = "user";
    }

    public function index( $c, $a ) {
        include $this->display( $c );
    }

    // 添加用户
    public function add( $c, $a ) {
        include $this->display( $c );
    }

    // 编辑用户
    public function edit( $c, $a ) {
        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }
        if ( !$result = $this->list[ $id ] ) {
            $this->error();
        }
        include $this->display( $c );
    }

    // 保存用户
    public function save( $c, $a ) {
        foreach ( $_POST as $k => $v ) {
            $rows[ $k ] = htmlspecialchars( trim( $v ) );
        }

        // 修改用户
        if ( $id = intval( R( 'id' ) ) ) {
            if ( $newpass = R( 'newpass' ) ) $rows[ 'pass' ] = md6( $newpass );
            $this->model->update( array( 'id' => $id ), $rows );
            $this->model->Cache()->update( 'user' );
            $this->success( '修改用户成功!' );
            return;
        }

        // 添加用户
        $rows[ 'pass' ] = md6( $rows[ 'pass' ] );
        $rows[ 'regdate' ] = time();
        $rows[ 'regip' ] = S( 'ip' );
        $rows[ 'actions' ] = '*';
        $this->model->create( $rows );

        // 更新缓存
        $this->model->Cache()->update( 'user' );
        $this->success( '添加用户成功!' );
    }

    // 删除用户
    public function del( $c ) {
        if ( !$id = intval( R( 'id' ) ) ) {
            $this->error();
        }
        if ( $id === 1 ) {
            $this->error( '不允许删除系统管理员!' );
        }

        $this->model->delete( array( 'id' => $id ) );

        // 更新缓存
        $this->model->Cache()->update( 'user' );

        $this->success( '删除用户成功!' );
    }
}